package com.drako.tdgdx.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Plane;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.Ray;
import com.drako.tdgdx.helper.CamController;
import com.drako.tdgdx.helper.GlobalHelper;
import com.drako.tdgdx.helper.VisualDebugHelper;
import com.drako.tdgdx.logic.GameLogic;

public class InputHandler implements InputProcessor {
	private CamController camControl;
	private PerspectiveCamera cam;
	private Ray ray;
	private Plane plane;
	private Vector3 intersection;
	private GameLogic logic;
	
	public InputHandler(PerspectiveCamera cam,GameLogic logic){
//		this.camControl = new CamController(cam);
		this.cam =cam;
		this.logic = logic;
		
		plane = new Plane(
							new Vector3(10,0,GlobalHelper.FIELD_ZDEPTH),
							new Vector3(0,10,GlobalHelper.FIELD_ZDEPTH),
							new Vector3(0,0,GlobalHelper.FIELD_ZDEPTH));
		intersection= new Vector3();
	}
	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		ray =cam.getPickRay(x, y);
		if(Intersector.intersectRayPlane(ray, plane, intersection)){
//			logic.getTowerHandler().buildTower(new LaserTower(GlobalHelper.posToArray(intersection.x), GlobalHelper.posToArray(intersection.y), 10, 10, logic.getPlayer()));
			Gdx.app.log("touchDown", "Intersect with Plane at: " + GlobalHelper.posToArray(intersection.x)+","+GlobalHelper.posToArray(intersection.y));
			Gdx.app.log("touchDown", "Intersect with Plane at: " + intersection.x+","+intersection.y);
			VisualDebugHelper.addPoint(new Vector2(intersection.x, intersection.y));
			
		}
		
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		
//		camControl.touchUp(x, y, pointer, button);
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
//		camControl.touchDragged(x, y, pointer);
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

}
